/**
 * 
 */
package com.gopay.webtest.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.webtest.controller.base.BaseController;
import com.gopay.webtest.util.BaseSign;
import com.gopay.webtest.vo.SignInfo;
import com.gopay.webtest.xml.XmlDao;

/**
 * @SignTradeController.java
 * @author pengxw
 * @2013-7-26 上午11:01:46  www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@RequestMapping("/unsign")
public class UnsignTradeController extends BaseController {

    private static Logger logger = Logger.getLogger(BackgroundMerController.class);
    private static String XML_FILE = "unsign";
    private static String FILE_tail = "unsignapply";
    private static String CHARSET_GBK = "1";
    @Autowired
    private XmlDao xmlDao;
    @Resource(name="Sign10000Handler")
    private BaseSign Sign10000Handler;
   /**
    * 设置签约报文的参数
    * @param res
    * @param req
    * @return
    */
    @RequestMapping(value = "/submit")
    public ModelAndView directUrl(HttpServletResponse res,  HttpServletRequest req)  {          
        ModelAndView mv = new ModelAndView("gopay.webtest.unsign");
        Map<String, String> signInfo= new HashMap<String, String>();
        try {
            signInfo=xmlDao.readData4Map(XML_FILE, FILE_tail);
            if(signInfo.get("tranDateTime")!=null){
                signInfo.put("tranDateTime", com.gopay.webtest.util.DateUtils.date2string(new Date(),com.gopay.webtest.util.DateUtils.DEFAULT_PATTERN ));
            }
            if(signInfo.get("tranIP")!=null){
                signInfo.put("tranIP", getRemoteRealIpAddr(req));
            }
        } catch (DocumentException e) {                     
            logger.warn(e.getMessage(), e);
        } catch (IOException e) {                    
            logger.warn(e.getMessage(), e);
        }
        mv.addObject("signInfo", signInfo);
        return mv;
    }
    /**
     * 提交签约报文的参数到网关
     * @param res
     * @param req
     * @return
     */
    @RequestMapping(value = "/redirect/submit")
    public ModelAndView redirectUrl(HttpServletResponse res,  HttpServletRequest req)  {          
        ModelAndView mv = new ModelAndView();
        SignInfo signInfo= new SignInfo();
        signInfo.setContractNo(getParameter("contractNo"));
       // signInfo.setContractNo("");        
        signInfo.setGopayServerTime(getParameter("gopayServerTime"));       
        signInfo.setMerchantID(getParameter("merchantID"));
        signInfo.setMerOrderNum(getParameter("merOrderNum"));
        signInfo.setMerURL(getParameter("merURL"));
        signInfo.setMsgExt(getParameter("msgExt"));
        signInfo.setRemark(getParameter("remark"));
        signInfo.setRemark1(getParameter("remark1"));
        signInfo.setRemark2(getParameter("remark2"));
        signInfo.setSignType(getParameter("signType"));
        signInfo.setTranCode(getParameter("tranCode"));
        signInfo.setTranIP(getParameter("tranIP"));
        signInfo.setVerficationCode(getVerifyCode());//
        signInfo.setVersion(getParameter("version"));
        signInfo.setTranDateTime(getParameter("tranDateTime"));
        signInfo.setGopayServerTime(getParameter("gopayServerTime"));
        signInfo.setCharset(getParameter("charset"));
        
       //"gopay.webtest.sign.redirect"
        String backgroundMerUrl =signInfo.getMerURL() + "?id="+ signInfo.getVerficationCode();
        signInfo.setMerURL(backgroundMerUrl);
        //MD5加密
        try {
            Sign10000Handler.sign(
                    signInfo.getTranCode()+"_"+signInfo.getVersion(),
                    "signType", "signValue", signInfo, req);
            System.out.println("后台notify密文值:" + signInfo.getSignValue());
        } catch (Exception e) {
            logger.warn(e.getMessage());
            e.printStackTrace();         
        }
        String signValue=signInfo.getSignValue();
       if(null==signValue){
        // 加密操作失败,置返回码为身份认证md5加密失败
           signInfo.setRespCode("MR08");
           mv.setViewName("gopay.webtest.unsign");
       }else{
           mv.setViewName("gopay.webtest.unsign.redirect");
       }
        mv.addObject("signInfo", signInfo);
        return mv;
    }
    
}
